/**
 * 资金登记单
 */

app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
	var gridId = $scope.gridId;
	
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				var listUkey = [];
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					var data = $scope.subGrid.sonlist[i];
					listUkey.push(data.vid);
				}
				$scope.showModalCheckBox('THR_OperateCompanyBankInfo', 'vid', listUkey);
				break;
			}
			case 'event-to-del': // 删除
			{
				$scope.subGrid.deleteBatch();
				break;
			}

	};
	}
}



/*
 * 子列表控制器
 */
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	/*
	 * 复选框回调
	 */
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {

		for (var i = 0; i < rowsSelected.length; i++) {
			var row = rowsSelected[i];
			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].vid == row.vid) {
					isAdd = false;
				}
			}
			if (isAdd) {
				var addRow ={
					id:'',
					paymentTime: '',
					operateCompanyBankInfoId : row.id,
					vid: row.vid,
					vyhzhmc: row.vyhzhmc,
					vkhh: row.vkhh,
					vyhzh: row.vyhzh,
					paymentAmount:0.00,
					accountType: row.accountType,
					accountTypeTxt: row.accountType!=null &&row.accountType==0?'对公户':'个人户',
					vjylx:'运营服务→预付款'
				};
				if(row.accountType==0){//对公户
					addRow.claimVgsmc = row.vyhzhmc;
				}
				$scope.subGrid.sonlist.push(addRow);
			}
		}

	}
}]);
app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	//监听子表数字输入框变更
	$scope.subGrid.numberChange = function(row, field) {
		if (field == 'paymentAmount') {

			//计算总金额
			var paymentAmount = 0 ;
			if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					if($scope.subGrid.sonlist[i].paymentAmount){
						paymentAmount = paymentAmount+parseFloat($scope.subGrid.sonlist[i].paymentAmount);
					}
				}
			}

			//回填
			$scope.valueMap['totalAmount'] = paymentAmount.toFixed(2);
		}
	}


}

